package com.wooophone.buyi.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.wooophone.buyi.dao.BaseDao;

public class BaseDaoImpl<T> implements BaseDao<T> {
	
	@Autowired
	protected MongoOperations mongoOperations;
	
	protected Class<T> entityClass;
	
	public MongoOperations getMongoOperations() {
		return mongoOperations;
	}

	public void setMongoOperations(MongoOperations mongoOperations) {
		this.mongoOperations = mongoOperations;
	}

	public Class<T> getEntityClass() {
		return entityClass;
	}

	public void setEntityClass(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	public BaseDaoImpl(Class<T> entityClass){
		this.entityClass=entityClass;
	}

	@Override
	public void save(T obj) {
		mongoOperations.save(obj);
	}

	@Override
	public void update(T obj) {
		mongoOperations.save(obj);
	}

	@Override
	public void delete(Object id) {
		mongoOperations.remove(Query.query(Criteria.where("_id").is(id)), entityClass);
	}

	@Override
	public List<T> list() {
		return mongoOperations.findAll(entityClass);
	}

	@Override
	public List<T> listByLimit(int pageNo, int pageSize) {
		return mongoOperations.find(new Query().limit(pageSize).skip((pageNo-1)*pageSize), entityClass);
	}

	@Override
	public long count() {
		return mongoOperations.count(new Query(), entityClass);
	}
	
	@Override
	public Criteria is(String key, Object value) {
		return Criteria.where(key).is(value);
	}

	@Override
	public Query isQuery(String key, Object value) {
		return Query.query(Criteria.where(key).is(value));
	}

	@Override
	public boolean exist(Object id) {
		return this.mongoOperations.count(isQuery("_id", id), entityClass) > 0;
	}

	@Override
	public T get(Object id) {
		return this.mongoOperations.findOne(isQuery("_id", id), entityClass);
	}

	@Override
	public T isResult(String key, Object value) {
		return this.mongoOperations.findOne(isQuery(key, value), entityClass);
	}

	@Override
	public List<T> isResults(String key, Object value) {
		return this.mongoOperations.find(isQuery(key, value), entityClass);
	}

	@Override
	public List<T> isResultsInclude(String key, Object value, String... fields) {
		Query query = isQuery(key, value);
		if (fields != null) {
			for (String string : fields) {
				query.fields().include(string);
			}
		}
		return this.getList(query);
	}

	@Override
	public List<T> isResultsExclude(String key, Object value, String... fields) {
		Query query = isQuery(key, value);
		if (fields != null) {
			for (String string : fields) {
				query.fields().exclude(string);
			}
		}
		return this.getList(query);
	}

	@Override
	public T get(Object id, String... fields) {
		Query query = isQuery("_id", id);
		if (fields != null) {
			for (String string : fields) {
				query.fields().exclude(string);
			}
		}
		return get(query);
	}

	@Override
	public T get(Query query) {
		return mongoOperations.findOne(query, entityClass);
	}

	@Override
	public List<T> getList(Query query) {
		return mongoOperations.find(query, entityClass);
	}

	@Override
	public T isResultInclude(String key, Object value, String... fields) {

		Query query = isQuery(key, value);

		if (fields != null) {
			for (String string : fields) {
				query.fields().include(string);
			}
		}
		return get(isQuery(key, value));
	}

	@Override
	public T isResultExclude(String key, Object value, String... excludes) {
		Query query = isQuery(key, value);
		if (excludes != null) {
			for (String string : excludes) {
				query.fields().exclude(string);
			}
		}
		return get(query);
	}

	@Override
	public List<T> getListInclude(Query query, String... fields) {
		if (fields != null) {
			for (String string : fields) {
				query.fields().include(string);
			}
		}
		return getList(query);
	}

	@Override
	public List<T> getListExclude(Query query, String... excludes) {
		if (excludes != null) {
			for (String string : excludes) {
				query.fields().exclude(string);
			}
		}
		return getList(query);
	}

	@Override
	public boolean exist(Query query) {
		return count(query) > 0;
	}

	@Override
	public long count(Query query) {
		return mongoOperations.count(query, entityClass);
	}
	
	
}
